package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacImageResult;
import com.ysd.lis.entity.bac.BacTestWorkFlow;
import com.ysd.lis.mapper.bac.BacImageResultMapper;
import com.ysd.lis.service.bac.BacImageResultService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-12-04
 */
@Service
public class BacImageResultServiceImpl extends ServiceImpl<BacImageResultMapper, BacImageResult> implements BacImageResultService {
    @Autowired
    BacImageResultMapper bacImageResultMapper;

    @Override
    public Result findBacImageResultList(BacImageResult bacImageResult) {
        List<BacImageResult> list = findBacImageResultArray(bacImageResult);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacImageResult> findBacImageResultArray(BacImageResult bacImageResult) {
        MPJLambdaWrapper<BacImageResult> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacImageResult::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getId()), BacImageResult::getId, bacImageResult.getId());
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getSampleNo()), BacImageResult::getSampleNo, bacImageResult.getSampleNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getRptGroup()), BacImageResult::getRptGroup, bacImageResult.getRptGroup());
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getIsolateNum()), BacImageResult::getIsolateNum, bacImageResult.getIsolateNum());
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getGermNo()), BacImageResult::getGermNo, bacImageResult.getGermNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getTestFlowNo()), BacImageResult::getTestFlowNo, bacImageResult.getTestFlowNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacImageResult.getTestNodeNo()), BacImageResult::getTestNodeNo, bacImageResult.getTestNodeNo());
        queryWrapper.eq(ToolsUtils.isNotEmpty(bacImageResult.getIsSelected()), BacImageResult::getIsSelected, bacImageResult.getIsSelected());
        if (ToolsUtils.isNotEmpty(bacImageResult.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacImageResult.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            queryWrapper.ge(BacTestWorkFlow::getSampleda, startTime);
            queryWrapper.le(BacTestWorkFlow::getSampleda, endTime);
        }
        return bacImageResultMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacImageResult(BacImageResult bacImageResult) {
        int i = bacImageResultMapper.insert(bacImageResult);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacImageResult);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacImageResult(BacImageResult bacImageResult) {
        LambdaQueryWrapper<BacImageResult> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacImageResult::getId, bacImageResult.getId());
        int i = bacImageResultMapper.delete(lambdaQueryWrapper);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacImageResult);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacImageResult(BacImageResult bacImageResult) {
        int i = bacImageResultMapper.updateById(bacImageResult);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacImageResult);
        }
        return Result.fail("更新失败");
    }
}
